package com.voxel.sense.infomsg.module.user.regist;

import android.text.TextUtils;

import com.example.com.common.app.BasePresenter;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.RegexUtils;
import com.example.com.common.utils.ResourceUtils;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.utils.constants.RegexConstants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.voxel.sense.infomsg.App;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.RegionBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.net.helper.NetHelper;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des :
 */
public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter,
        DataSource.Callback<BaseEntity> {


    private List<RegionBean> mDatas;

    public RegisterPresenter(RegisterContract.View view) {
        super(view);
    }


    @Override
    public void doRegister(String username, String psw, String pswAgain, String regionCode, String idCard,
                           String realName, String phone, String email) {

        if (!NetworkUtils.isConnected()) {
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.network_unavailable));
        }
//        else if (TextUtils.isEmpty(regionCode)) {
//            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_login_invalid_parameter));
//        }
        else if (TextUtils.isEmpty(username) || TextUtils.isEmpty(psw)) {
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_login_invalid_parameter));
        } else if (!RegexUtils.isMatch(RegexConstants.REGEX_USERNAME_SINGGLE, username)) {
            //检查用户名是否匹配要求
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_invalid_parameter_name));
        } else if (!RegexUtils.isMatch(RegexConstants.REGEX_PASSWORD, psw)) {
            //检查密码格式是否匹配
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_invalid_parameter_password));
        } else if (!psw.equals(pswAgain)) {
            //检查两次密码输入是否一致
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_two_psw_no_equal));
        } else if (TextUtils.isEmpty(realName)) {
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_invalid_parameter_realname));
        } else if (TextUtils.isEmpty(idCard) || !RegexUtils.isIDCard18(idCard)) {
            //检查身份证号码格式是否正确
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_invalid_parameter_idcard));
        } else if (TextUtils.isEmpty(phone) || !RegexUtils.isMobileExact(phone)) {
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_invalid_parameter_phone));
        } else if (!TextUtils.isEmpty(email) && !RegexUtils.isEmail(email)) {
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.user_account_register_invalid_parameter_email));
        } else {
            //提交请求
            NetHelper.userRegister(username, psw, regionCode, realName, phone, email, idCard, this);
        }
    }

    @Override
    public void loadProvinceData() {
        String s = ResourceUtils.readAssets2String("areaDataCode.json");
        Gson gson = new Gson();

        if (mDatas == null) {
            mView.showLoading("");
            Type type = new TypeToken<List<RegionBean>>() {
            }.getType();
            mDatas = gson.fromJson(s, type);
        }
        //城市列表
        List<List<RegionBean.ChildBeanX>> city = new ArrayList<>();
        //区级列表数据
        List<List<List<RegionBean.ChildBeanX.ChildBean>>> districk = new ArrayList<>();
        if (!ListUtil.isEmpty(mDatas)) {
            //第一层级遍历省级列表
            for (RegionBean regionBean : mDatas) {
                List cityList = new ArrayList();
                List<RegionBean.ChildBeanX> childList = regionBean.getChild();
                if (!ListUtil.isEmpty(childList)) {
                    //遍历城市级别列表
                    for (RegionBean.ChildBeanX childBean : childList) {
                        List<RegionBean.ChildBeanX.ChildBean> childs = childBean.getChild();
                        if (ListUtil.isEmpty(childs)) {
                            //避免最后一级的数据为空，引起异常。
                            childs = new ArrayList<>();
                            RegionBean.ChildBeanX.ChildBean bean = new RegionBean.ChildBeanX.ChildBean();
                            bean.setId(childBean.getId());
                            bean.setIp(childBean.getIp());
                            bean.setName("-");
                            childs.add(bean);
                        }
                        cityList.add(childs);
                    }
                } else {
                    //若在市级数据是空，这里最好添加空字符或作其它处理，以免出现null，导致异常发生
                    childList = new ArrayList<>();
                    RegionBean.ChildBeanX childBean = new RegionBean.ChildBeanX();
                    childBean.setId(regionBean.getId());
                    childBean.setName("-");
                    childBean.setIp(regionBean.getIp());
                    childList.add(childBean);

                    List<RegionBean.ChildBeanX.ChildBean> childs = new ArrayList<>();
                    RegionBean.ChildBeanX.ChildBean bean = new RegionBean.ChildBeanX.ChildBean();
                    bean.setId(childBean.getId());
                    bean.setIp(childBean.getIp());
                    bean.setName("-");
                    childs.add(bean);
                    cityList.add(childs);
                }
                city.add(childList);
                districk.add(cityList);
            }
        }
        mView.dimissLoading();
        mView.onShowRegionPickerView(mDatas, city, districk);
    }

    @Override
    public void onDataLoaded(BaseEntity data, int reqId) {
        if (mView != null) {
            mView.dimissLoading();
            if (data.isSuccess()) {
//                mView.showSnackBarWarning("注册成功");
                mView.onRegistSuccess();
            } else {
                mView.showSnackBarWarning(data.msg);
            }
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView != null) {
            mView.dimissLoading();
            mView.showSnackBarWarning(App.getInstance().getResources().getString(R.string.network_error));
        }
    }
}
